/**
 * Description: 十六进制字符串转长整型
 * File: hex_string_to_long.cpp |Time: 2022-05-12 09:14
 */

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

/**
 * 16进制字符串转长整型
 */
long hex2long(const string &hexStr)
{
    char *offset;
    if(hexStr.length() > 2){
        if(hexStr[0] == '0' && (hexStr[1] == 'x' || hexStr[1] == 'X')) {
           return strtol(hexStr.c_str(), &offset, 0); 
        }
    }
    
   return strtol(hexStr.c_str(), &offset, 16); 
}

int main(int argc, const char *argv[])
{
    string s = "a12";
    cout << "string: " << s << endl;
    cout << "long: " << hex2long(s) << endl;

    return 0;
}

